const baseUrl = "http://47.108.197.28:4000"
class Http{
    static request({
        url,
        data,
        method ="get"
    }){
        return new Promise((resolve,reject)=>{
            wx.request({
                url: baseUrl+url,
                data,
                header: {'content-type':'application/json',apikey:""},
                method,
                dataType: 'json',
                responseType: 'text',
                success: (res)=>{
                    this.handleStatusCode(res,resolve)
                },
                fail: (res)=>{
                    this.handleStatusCode(res,reject)
                },
            });
        })
    }

    static handleStatusCode(res,params){
        console.log(res.statusCode)
        console.log(res.data.code)
        if(res.statusCode){
            if(res.statusCode ==200 && res.data.code == 200){
                params(res)
            }else{
                wx.showToast({
                    title: '异常',
                });
                throw new Error("传参异常")
            }
        }else{
            //连接不上网络
            params(res);
            wx.showToast({
                title: '无法连接网络',
            })
            throw new Error("网络异常")
        }
    }
}
module.exports = Http